package br.com.sig.financeiro.locacao.presentation.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;

import br.com.sig.criteria.CriteriaVisitor;
import br.com.sig.financeiro.locacao.business.model.bo.LocacaoBO;
import br.com.sig.financeiro.locacao.business.model.entity.Locacao;
import br.com.sig.presentation.action.BaseListAction;

@Named
@ViewScoped
public class LocacaoList extends BaseListAction<Locacao> {
	
	private static final long serialVersionUID = -7646544763710022532L;
	
	private Date dataDeInicio = null;
	private Date dataDeTermino = null;
	private Date dataDePagamentoInicio = null;
	private Date dataDePagamentoFim = null;
	
	@Inject
	private LocacaoBO locacaoBO;
	
	@PostConstruct
	public void init() {
		setService(locacaoBO);
	}
	
	@Override
	public void find() {		
		this.visitor = 
				new CriteriaVisitor() {
					@Override
					public void visit(Criteria criteria) {
						
						if (dataDeInicio != null) {
							DateTime dateTime = new DateTime(dataDeInicio.getTime());
							dateTime = dateTime.hourOfDay().withMinimumValue();
							criteria.add(Restrictions.ge("dataDeInicio", dateTime.toDate()));
						}
						
						if (dataDeTermino != null) {
							DateTime dateTime = new DateTime(dataDeTermino.getTime());
							dateTime = dateTime.hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue().secondOfMinute().withMaximumValue();
							criteria.add(Restrictions.le("dataDeTermino", dateTime.toDate()));							
						}
						
						if (dataDePagamentoInicio != null) {
							DateTime dateTime = new DateTime(dataDePagamentoInicio.getTime());
							dateTime = dateTime.hourOfDay().withMinimumValue();
							criteria.add(Restrictions.ge("dataDePagamento", dateTime.toDate()));
						}
						
						if (dataDePagamentoFim != null) {
							DateTime dateTime = new DateTime(dataDePagamentoFim.getTime());
							dateTime = dateTime.hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue().secondOfMinute().withMaximumValue();
							criteria.add(Restrictions.le("dataDePagamento", dateTime.toDate()));							
						}
					}
				};
				
		super.find();
	}
	
	@Override
	protected void clearInstance() {
		super.clearInstance();
		this.dataDeInicio = null;
		this.dataDeTermino = null;
	}
	
	public BigDecimal getValorTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (Locacao locacao : (List<Locacao>)getPagedResult().getWrappedData()) {
			if (locacao.getValor() != null) {
				total = total.add(locacao.getValor());
			}
		}
		
		return total;
	}
	
	public BigDecimal getBvTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (Locacao locacao : (List<Locacao>)getPagedResult().getWrappedData()) {
			if (locacao.getValorBonificacaoDeVenda() != null) {
				total = total.add(locacao.getValorBonificacaoDeVenda());
			}
		}
		
		return total;
	}
	
	public BigDecimal getComissaoTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (Locacao locacao : (List<Locacao>)getPagedResult().getWrappedData()) {
			if (locacao.getComissao()!= null) {
				total = total.add(locacao.getComissao());
			}
		}
		
		return total;
	}
	
	public BigDecimal getFreteTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (Locacao locacao : (List<Locacao>)getPagedResult().getWrappedData()) {
			if (locacao.getValorFrete()!= null) {
				total = total.add(locacao.getValorFrete());
			}
		}
		
		return total;
	}
	
	public BigDecimal getReceitaTotal() {
		BigDecimal total = new BigDecimal(0);
		
		for (Locacao locacao : (List<Locacao>)getPagedResult().getWrappedData()) {
			if (locacao.getReceita() != null) {
				total = total.add(locacao.getReceita());
			}
		}
		
		return total;
	}
	
	public BigDecimal getDespesaTotal() {
		return getBvTotal().add(getComissaoTotal()).add(getFreteTotal());
	}

	public Date getDataDeInicio() {
		return dataDeInicio;
	}

	public void setDataDeInicio(Date dataDeInicio) {
		this.dataDeInicio = dataDeInicio;
	}

	public Date getDataDeTermino() {
		return dataDeTermino;
	}

	public void setDataDeTermino(Date dataDeTermino) {
		this.dataDeTermino = dataDeTermino;
	}

	public Date getDataDePagamentoInicio() {
		return dataDePagamentoInicio;
	}

	public void setDataDePagamentoInicio(Date dataDePagamentoInicio) {
		this.dataDePagamentoInicio = dataDePagamentoInicio;
	}

	public Date getDataDePagamentoFim() {
		return dataDePagamentoFim;
	}

	public void setDataDePagamentoFim(Date dataDePagamentoFim) {
		this.dataDePagamentoFim = dataDePagamentoFim;
	}
}